library(lattice)
library(foreign)
library(reshape2)
library(MASS)
library(latticeExtra)
library(ggplot2)
library(ggthemes)

###################################################
###################################################
##################################################
# densities of change in diffuse support 
###################################################  
###################################################
###################################################  


densities2 <- read.dta("for densities2.dta")
densities2 <- melt(densities2)

plot1 <- (
  densityplot(~ as.numeric(value) | variable,
              data = densities2,
              aspect = 1,
              col = "black",
              plot.points = FALSE,
              xlab = "Change in Diffuse Support",
              index.cond=list(c(2,1,5,3,6, 4)),
              strip=strip.custom(var.name="variable", 
                                 factor.levels=c("Romney", "O'Reilly", "Cooper", "Maddow", 
                                                 "Schumer", "Limbaugh")),
              strip.levels=rep(TRUE,3)
  )  
)  


control <- read.csv("control.csv")

plot2 <- (
  densityplot(~ as.numeric(value) | variable,
              data = control,
              aspect = 1,
              col = "grey",
              lty = 2,
              plot.points = FALSE,
              xlab = "Change in Diffuse Support",
              index.cond=list(c(2,1,5,3,6, 4)),
              strip=strip.custom(var.name="variable", 
                                 factor.levels=c("Romney", "O'Reilly", "Cooper", "Maddow", 
                                                 "Schumer", "Limbaugh")),
              strip.levels=rep(TRUE,3)
  )  
)  

plot2 + plot1 
trellis.focus("toplevel") ## has coordinate system [0,1] x [0,1]
panel.text(.57, .74, "Control", cex = .7, font = 2)
trellis.unfocus()


##############################
##############################
##############################
## effects plot of change in diffuse 
## support across affect 
##############################
##############################
##############################

effects  <- read.csv("affect plot.csv")

xyplot(val ~ x | person, 
       data = effects,
       group = group,
       xlab = "Affect",
       ylab = "Change in Diffuse Support",
       aspect = 1,
       type = "l",
       lty = c(1, 2, 2),
       index.cond=list(c(4, 5, 6, 1, 2, 3)),
       col = c("black", "black", "black"),
       panel = function(x, y, ...) {
         panel.xyplot(x, y,...)
         panel.lines(x=-1:11, y=0, col="grey", lty = 1)
       }
)       




##############################
##############################
##############################
## effects plot of change in diffuse
## support across ideological change
##############################
##############################
##############################

ideo <- read.csv("ideo plot.csv")

xyplot(est ~ x | person, 
       data = ideo,
       group = group,
       xlab = "Change in Ideological Distance",
       ylab = "Change in Diffuse Support",
       aspect = 1,
       type = "l",
       lty = c(1, 2, 2),
       layout = c(3,2),
       index.cond=list(c(4, 5, 6, 1, 2, 3)),
       col = c("black", "black", "black"),
       panel = function(x, y, ...) {
         panel.xyplot(x, y,...)
         panel.lines(x=-4:5, y=0, col="grey", lty = 1)
       }
)       



##############################
##############################
##############################
## effects plot of change in diffuse
## support across affect
## for control group 
##############################
##############################
##############################

controlAf <- read.csv("control affect.csv")
controlAf$var <- reorder(controlAf$var, controlAf$est)

dotplot(var ~ est, 
        data = controlAf,
        aspect = 1.5,
        xlim = c(-.05, .055),
        xlab = "Affect Coefficient",
        panel = function (x, y) {
          panel.xyplot(x, y, pch = 16, col = "black")
          panel.lines(x = 0, y = -1:7, col = "grey", lty = 2)
          panel.segments(controlAf$lb, as.numeric(y),
                         controlAf$ub, as.numeric(y), lty = 1, col = "black")} )




